package org.zdd.bookstore.web.controller;

import net.sf.ehcache.search.impl.BaseResult;
import org.zdd.bookstore.common.pojo.BSResult;
import org.zdd.bookstore.common.utils.BSResultUtil;
import org.zdd.bookstore.model.dao.BookDescMapper;
import org.zdd.bookstore.model.entity.CartBook;
import org.zdd.bookstore.model.entity.User;
import org.zdd.bookstore.model.entity.custom.Cart;
import org.zdd.bookstore.model.entity.BookInfo;
import org.zdd.bookstore.model.service.IBookInfoService;
import org.zdd.bookstore.model.service.ICCartService;
import org.zdd.bookstore.model.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private IBookInfoService bookInfoService;

    @Autowired
    private ICartService cartService;

    @Autowired
    private ICCartService icCartService;
    //返回购物差页面
    @GetMapping("/items")
    public String showCart(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("loginUser");
        System.out.println(request);
        int id =user.getUserId();
        System.out.println(id);
        BSResult bsResult = icCartService.queryByUserId(id);
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        System.out.println(cart);
        System.out.println(bsResult.getData());
        request.getSession().setAttribute("cart", bsResult.getData());
        icCartService.initCart(id);

//        request.setAttribute("cart",bsResult.getData());
        return "cart";
    }
//    @GetMapping("/iteml")
//    public String showCart( HttpServletRequest request) {
//        User user = (User) request.getSession().getAttribute("loginUser");
//        int id =user.getUserId();
//        Cart cart = icCartService.queryByUserId(id);
//        request.setAttribute("cart",cart);
//        return "cart";
//    }

    /**
     * 加入购物车
     *
     * @param bookId
     * @param request
     * @return
     */
    @RequestMapping("/addition")
    public String addToCart(@RequestParam(value = "bookId",defaultValue = "0") int bookId,
                            @RequestParam(required = false,defaultValue = "0") int buyNum,
                            HttpServletRequest request) {
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        System.out.println(request);

        System.out.println("cart");
        User user = (User) request.getSession().getAttribute("loginUser");
        int id =user.getUserId();
        System.out.println(id);
        icCartService.addToCart(id,bookId,buyNum);
        //根据要加入购物车的bookId查询bookInfo
        BookInfo bookInfo = bookInfoService.queryBookAvailable(bookId);

        if (bookInfo != null) {
            //这本书在数据库里
            BSResult bsResult = cartService.addToCart(bookInfo, cart, buyNum);
            request.getSession().setAttribute("cart", bsResult.getData());
            request.setAttribute("bookInfo", bookInfo);
        } else {
            //数据库里没有这本书,或库存不足
            request.setAttribute("bookInfo", null);
        }
        return "addcart";
    }

    @GetMapping("/clear")
    public String clearCart(HttpServletRequest request) {
        cartService.clearCart(request,"cart");
        return "cart";
    }

    @GetMapping("/deletion/{bookId}")
    public String deleteCartItem(@PathVariable("bookId") int bookId,HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("loginUser");
        int id =user.getUserId();
        icCartService.deleteCartItem(bookId,id);
        cartService.deleteCartItem(bookId, request);
        return "redirect:/cart/items";
    }

    /**
     * 更新某个购物车项的购买数量
     * @param bookId
     * @param newNum
     * @param request
     * @return
     */
    @PostMapping("/buy/num/update")
    @ResponseBody
    public BSResult updateBuyNum(int bookId, int newNum, HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("loginUser");
        int id =user.getUserId();
        icCartService.updateBuyNum(id,bookId,newNum);
        return cartService.updateBuyNum(bookId, newNum, request);
    }

    @PostMapping("/checkOne")
    @ResponseBody
    public String checkACartItem(int bookId, HttpServletRequest request, HttpServletResponse response){
        Cart cart = (Cart)request.getSession().getAttribute("cart");
        User user = (User) request.getSession().getAttribute("loginUser");
        int id =user.getUserId();
        BSResult bsResult = icCartService.checkedOrNot(cart,id,bookId);
        request.getSession().setAttribute("cart",bsResult.getData());

//        return cartService.checkedOrNot(cart, bookId);
        return "cart";
    }
}
